#!/bin/sh 

echo $0 $* >> /tmp/monis
. /usr/voltaire/common.sh

LOG_MAX_LINES=1024
log(){
	if [ -z $log_file ] ; then
		echo $* 
	else
		echo "$(date +"%D %R") $log_pfx $*" >> $log_file
	fi
}

usage(){
	log usage: "<--dev dev-name> <--port port-num> <--interface ib-if> [--startif] [--overwrite] [--backup] [--logfile log-file] [--logprefix prefix] [--fake] [--nocolor]"
	exit 1
}


overwrite=
backup=
start=0
method_dhcp=1
nopurge=0

while [[ ! -z "$1" ]] ; do
        case $1 in
                --startif)
                        start=1
                        shift
                ;;
                --overwrite)
                        overwrite=-overwrite
                        shift
                ;;
                --backup)
                        backup=-backup
                        shift
                ;;
                --fake)
                        fake=echo
                        shift
                ;;
                --nocolor)
			nocolor=-nocolor
                        shift
                ;;
                --static)
			method_dhcp=0
                        network=$2
                        shift
                        shift
                ;;
                --dev)
                        dev_name=$2
                        shift
                        shift
                ;;
                --port)
                        port_num=$2
                        shift
                        shift
                ;;
                --interface)
                        ib_if=$2
                        shift
                        shift
                ;;
                --nopurge)
                        nopurge=1
                        shift
                ;;
                --logprefix)
                        log_pfx=$2:
                        shift
                        shift
                ;;
                --logfile)
                        log_file=$2
                        shift
                        shift
                ;;
                *)
                        log "unknown switch $1"
                        usage
                        exit
                ;;
        esac
done

rm -rf /tmp/gvd-$$
mkdir /tmp/gvd-$$
get_ib_netscripts
if [ ! -z "$ib_netscripts" ] ; then
	for s in $ib_netscripts ; do
		mv $netscript_dir/ifcfg-$s /tmp/gvd-$$
	done
fi


if [ ! -z $log_file ] ; then 
	if [ -f $log_file ] ; then
		n_log_lines=$(wc -l $log_file|cut -f1 -d" ")
		if [ $n_log_lines -ge $LOG_MAX_LINES ] ; then
			rm -f $log_file
		fi
	fi
fi

if [ -z $dev_name ] ; then
	log ERROR: device name not specified
	exit 1
fi

if [ -z $port_num ] ; then
	log ERROR: port num not specified
	exit 1
fi

if [ -z $ib_if ] ; then
	log ERROR: parent IB interface not specified
	exit 1
fi

if [ ! -d /sys/class/infiniband/$dev_name ] ; then
	log ERROR: no entry for $dev_name under /sys/class/infiniband/
	exit 1
fi

#if [ ! -d /sys/class/infiniband/$dev_name/device/net:$ib_if ] ; then
#	log ERROR: no entry for $ib_if under /sys/class/infiniband/$dev_name/device/
#	exit 1
#fi

if [ ! -d /sys/class/infiniband/$dev_name/ports/$port_num ] ; then
	log ERROR: no entry for $port_num under /sys/class/infiniband/$dev_name/ports/
	exit 1
fi

sysfs_pkey_base=/sys/class/infiniband/$dev_name/ports/$port_num/pkeys

for (( i=0; i<64; i++ )) ;  do
	pkey=$(cat $sysfs_pkey_base/$i|cut -f2 -dx)
	if [ $pkey != "0000" ] ; then
		if [ $pkey != "ffff" ] ; then
			if [ $method_dhcp -ne 0 ] ; then
				cmdout=$($fake /usr/local/bin/ib-config-as-eth -dhcp -ib $ib_if.$pkey $overwrite $backup $nocolor 2>/dev/null)
			else
				cmdout=$($fake /usr/local/bin/ib-config-as-eth -network $network -usepkey -ib $ib_if.$pkey $overwrite $backup $nocolor 2>/dev/null)
			fi
			if [ $? -ne 0 ] ; then
				log ERROR: failed to configure $ib_if.$pkey $cmdout
			else
				log Configuration of $ib_if.$pkey succeeded.
				if [ $start -ne 0 ] ; then
					ifup $ib_if.$pkey
				fi
				rm -f /tmp/gvd-$$/ifcfg-$ib_if.$pkey 
			fi
		fi
	fi
		
done

old_scripts=$(ls /tmp/gvd-$$/ifcfg-* 2>/dev/null)
if [ ! -z "$old_scripts" ] ; then
	for f in $old_scripts ; do 
		$netscript_dir/ifcfg-$s
		f=$(basename $f)
		ib=$(echo $f|cut -f2 -d-)
		cp /tmp/gvd-$$/$f $netscript_dir/
		ifdown $ib
		rm -f $netscript_dir/$f
	done
	rm -rf /tmp/gvd-$$/
fi

exit 0

